#include "task_cycle.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include  <stdint.h>
#include "db_op.h"
#include "cJSON.h"
#include <sys/types.h>
#include <sys/stat.h> 


int getHeaderField(const char* data,const char* field,char * value){
    //char tmp[128]={0};
    if(value==NULL) return FAIL;
    memset(value,0,32);
    char* start = strstr(data,field);
    char* end = strchr(start,'\n');
    if((start == NULL) || (end == NULL))
    {
        printf("can't find %s\n",field);
        return FAIL;
    }

    strncpy(value,start+strlen(field),end-start-strlen(field));
    printf("%s is %s\n",field,value);
    //strstr(request_header,"Length:")
    return SUCCESS;
}
int getReqHeader(int fd, REQ_HEADER* req_header)
{
    int n, rc;
    char c;//, *ptr;
    char splitchr[4]={0};
    //ptr = (char*)vptr;
    //char ptr[sizeof(REQ_HEADER)+2]={0};
    char *data=(char*)malloc(sizeof(char)*(sizeof(REQ_HEADER)+2));
    char* ptr=data;
    for(n = 1 ;n < sizeof(char)*(sizeof(REQ_HEADER)+2); n++)
    {
        if( (rc = read(fd, &c, 1)) == 1)
        {
            *ptr++ = c;
            if(c == '\r')
            {
                if(( (rc = read(fd, splitchr, 3)) == 3))
                {
                    if((splitchr[0]=='\n')&&(splitchr[1]=='\r')&&(splitchr[2]=='\n'))
                    {
                        *ptr = '\0';
                        break;
                    }
                }
            }
        }else if( rc == 0) {
            *ptr = 0;
            //return (n-1);
        }else{
            return FAIL;
        }
    }

    char value[32]={0};
    rc=getHeaderField(data,"Length:",value);
    if(rc==FAIL){
        return FAIL;
    }
    req_header->length=atoi(value);

    rc=getHeaderField(data,"Method:",value);
    if(rc==FAIL){
        return FAIL;
    }
    req_header->method=atoi(value);

    rc=getHeaderField(data,"Uid:",value);
    if(rc==FAIL){
        return FAIL;
    }
    req_header->uid=atoll(value);

    rc=getHeaderField(data,"Fname:",value);
    if(rc==FAIL){
        value[0]='\0';
    }
    strncpy(req_header->fname,value,sizeof(req_header->fname));

    free(data);
    return SUCCESS;
}


int saveRecData(struct epollfd_priv *priv,REQ_HEADER* req_header,char* data)
{
    char file_path[120] = {0};
    sprintf(file_path, "/var/emu/%lu/",req_header->uid);
    if (access(file_path, F_OK))//不存在
    {
        printf("11111111111111111\n");
        if(mkdir(file_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)){
            perror("mkdir fail");
        }
    }
    strcat(file_path,req_header->fname);
    FILE* fp=fopen(file_path,"w+");
    if(fp == NULL)
    {
        printf("open file failed! path is: %s\n",file_path);
        perror("saveRecData");
        return FAIL;
    }

    fwrite(data,strlen(data),1,fp);
    fclose(fp);

	//rev = add_emu_task(&task, HANDLE_TAIL);
	//printf("add_emu_task rev=%d\n",rev);
    TaskInfo ti;
    ti.uid=req_header->uid;
    strncpy(ti.fname,req_header->fname,sizeof(ti.fname));
    strncpy(ti.path,file_path,sizeof(ti.path));
    ti.status=0;
    add_task(&ti);

	if(data){
        free(data);
    }
    //send_to_user_cb(fd,"200 OK");
    return SUCCESS;
}

void add_header(char* out,char* data){
    int len=strlen(out);
    sprintf(data,"Length:%d\n\r\n\r\n",len);
    strcat(data,out);
}

int getFileList(struct epollfd_priv *priv,REQ_HEADER* req_header)
{
    TaskInfo *ti=NULL;
    char* out;
    char* data[MAXSIZE]={0};
    int i=0;
    int size=0;
    cJSON *root,*fld;
    root=cJSON_CreateArray();
    get_file_list(req_header->uid,&ti,&size);
    for(i=0;i<size;i++){
        printf("--------------%lu\n",ti[i].uid);
        cJSON_AddItemToArray(root,fld=cJSON_CreateObject());
        cJSON_AddNumberToObject(fld, "uid", ti[i].uid);
        cJSON_AddStringToObject(fld, "fname", ti[i].fname);
    }
    out=cJSON_Print(root);
    cJSON_Delete(root);
    printf("%s\n",out);
    add_header(out,data);
    send_to_user_cb(priv,data);
    free(out); 
    free(ti);
    return 0;
}
